const {
  SyncHook,
  SyncBailHook,
  SyncLoopHook,
  SyncWaterfallHook,
  AsyncParallelHook,
} = require("tapable");

class Compiler {
  constructor() {
    this.hook = {
      // 1.创建hooks
      syncHook: new SyncHook(["name", "age"]),
      // bail的特点: 如果有返回值, 那么可以阻断后续事件继续执行
      bailHook: new SyncBailHook(["name", "age"]),
      // loop的特点: 如果有返回值, 那么会继续执行, 直到没有返回值为止
      loopHook: new SyncLoopHook(["name", "age"]),
      // waterfall的特点: 如果有返回值, 那么会继续执行, 并且将返回值传递给下一个事件
      waterfallHook: new SyncWaterfallHook(["name", "age"]),
      // AsyncParallel的特点: 异步并行, 不会等待事件执行完毕, 不会阻塞后续事件执行
      asyncParallelHook: new AsyncParallelHook(["name", "age"]),
    };

    // 2.用hooks监听事件(自定义plugin)
    this.hook.asyncParallelHook.tapAsync("event1", (name, age) => {
      setTimeout(() => {
        console.log("event1:", name, age);
      }, 3000);
    });

    this.hook.asyncParallelHook.tapAsync("event2", (name, age) => {
      setTimeout(() => {
        console.log("event2:", name, age);
      }, 3000);
    });
  }
}

const compiler = new Compiler();
setTimeout(() => {
  compiler.hook.asyncParallelHook.callAsync("zhangsan", 18);
}, 2000);
